# -*- coding: utf-8 -*-

__author__ = "Sérgio Severino, 9733"
__version__ = "v1.0 from 2/12/2011"

import urllib
from BeautifulSoup.BeautifulSoup import BeautifulSoup



class UrlTagCounter:
    '''
    A class with a simgle static method
    it receives a tag list and an url
    then returns the quantity of each tag in the site
    returns -1 if the tag's list is empty
    returns -2 if the url is invalid
    '''
    @staticmethod
    def countTags( tagList, url ):
        if not tagList:
            return -1
        
        tags = []
            
        try:
            f = urllib.urlopen(url)
            soup = BeautifulSoup(f)
            f.close()
            
        
            for tag in tagList:
                try:
                    temp = [tag]
                    quantity = 0
                    # if the tag is p, getattr(soup, tag) is soup.p
                    result = getattr(soup, tag) # same as: soup.<the inside of tag>
                    for i in result:
                        quantity = quantity + 1
                        pass
                except TypeError:
                    temp.append( 0 )
                    pass
                else:
                    temp.append( quantity )
                    pass
                tags.append(temp)
                pass

        except IOError:
            return -2
            pass
        return tags
        pass

